Introduction to Poetry

Motivation

  • efficient Python project management encompasses multiple crucial steps:
    • managing Python versions
    • managing virtual environments
    • managing dependencies
    • package building
    • package publishing
  • poetry covers almost all these steps

Tools Diagram Tools Diagram

Source

Python Version Management

  • not supported with poetry
  • instead use pyenv
  • in poetry config set virtualenvs.prefer-active-python to true to make pyenv work with poetry

Environment Management

  • virtual environments crucial for dependency management
  • poetry creates and activates virtual environments for you

Dependency Management

Why?

  • consistency, stability, collaboration, …

works_on_my_machine works_on_my_machine

Library vs. Application

  • Application: versions of dependencies pinned
  • Library: more flexibility needed

Dependency Management in poetry

  • dependencies are specified in pyproject.toml file
  • dependencies can be added by running
poetry add <package_name>
  • poetry creates a lock file where all versions are pinned
  • packages can be installed using the lock file
poetry install

Package Building

  • before publishing, a package needs to be build
  • building will create a source distribution and a built distribution
poetry build

Package Publishing

  • last step is publishing the package
  • poetry will publish to PyPI by default
  • credentials need to be configured properly
poetry publish